using System;
using System.Collections.Generic;

namespace KodeIT.Web
{
    /// <summary>
    /// This class represents the collection that provides access to the server variables.
    /// </summary>
    public class ServerVariables
    {
        private NativeContext _nativeContext;
        private Dictionary<string, string> _getCollection;

        internal ServerVariables(NativeContext nativeContext)
        {
            _nativeContext = nativeContext;
            _getCollection = new Dictionary<string, string>(NativeMethods.MIN_VARIABLE_COUNT);
        }

        /// <summary>
        /// Gets the value of a server variable.
        /// </summary>
        /// <param name="name">The name of the server variable.</param>
        /// <returns>The value of the server variable.</returns>
        public string Get(string name)
        {
            if (!_getCollection.ContainsKey(name))
            {
                _getCollection[name] = NativeMethods.GetVariable(_nativeContext, name);
            }

            return _getCollection[name];
        }

        /// <summary>
        /// Gets the value of a server variable.
        /// </summary>
        /// <param name="name">The name of the server variable.</param>
        /// <returns>The value of the server variable.</returns>
        public string this[string name]
        {
            get { return Get(name); }
        }
    }
}
